<template>
  <div class="relayArea" v-if="showData" :style="{backgroundColor: (step=='view'?'#fff':$store.state.mainColor)}">
    <enterRelay v-if="step =='index'" :basicData="basicData" :relayData="relayData" :actid="actid" @stepChange="stepChange"></enterRelay>
    <relayView v-if="step == 'view'" :relayData="relayData" :actid="actid" @stepChange="stepChange"></relayView>
    <relaySign v-if="step == 'sign'" :relayData="relayData" :actid="actid" @stepChange="stepChange"></relaySign>
    <relayPoster ref="posterPage" v-if="step == 'poster'" :relayData="relayData" :actid="actid" @stepChange="stepChange"></relayPoster>
    <posterShow v-if="step == 'posterShow'" :relayData="relayData" :actid="actid" :posterData="posterData" @stepChange="stepChange" @setPosterShare="setPosterShare"></posterShow>
    <audioSet ref="audioCon" :relayData="relayData" v-if="open_music"></audioSet>
  </div>
</template>

<script>
import enterRelay from "../../../components/relay/enterRelay";
import relaySign from "../../../components/relay/relaySign";
import relayPoster from "../../../components/relay/relayPoster";
import relayView from "../../../components/relay/relayView";
import posterShow from "../../../components/relay/posterShow"
import {mapState} from "vuex";
import {ActivityInfo, activityText} from '@/api/api.js'
import {isWeiXin,isQianfan,checkPlatform,shareFun,setShare,newJumpUrl,proxyShareFun} from '@/until/QFH5.js'
import audioSet from "../../../components/relay/audioSet";
import {checkImage} from "../../../until/QFH5";
import watermarkOption from '@/until/water.js'
import {strChange} from '@/until/tool'
import wxModule from "jweixin-module";
export default {
  name: "relay",
  data() {
    return {
      isframe: false,
      showData:false,
      posterData:{},
      actid:0,
      open_music:true,
      relayData:{},
      basicData:{},
      /*showPage:'index',*/
    }
  },
  computed: {
    ...mapState({
      step: state => state.step,
    })
  },
  components: {
    enterRelay,
    relayView,
    relaySign,
    relayPoster,
    posterShow,
    audioSet
  },
  methods: {
    stepChange(type,posterData) {
	  let path = type
	  let {open_theme,part_images} = this.relayData.settings
	  if(path=='view' && open_theme && part_images.length==0){
		  path = 'sign'
	  }
	  this.$store.state.step = path
      if('posterShow' == path){
        this.posterData = posterData
        this.$refs['posterPage'].changeClick()
      }
    },
    async getInit() {

      let res = await activityText({actid: this.actid})
      let basicData = res.data
      this.basicData = basicData
      this.$store.commit('SET_BASIC_DATA', basicData)
      if (!this.isframe) {
        await this.getInitData()
      }else{
        this.getMessage()
      }

    },
    getMessage(){
      let _this = this

      window.addEventListener('message',function(e){
        _this.showData = false
        if(e.data.type != 'webpackWarnings'){
          let relayData = Object.assign({},e.data)
          if(0 == relayData.status){
            watermarkOption.set('活动未发布')
          }
          /*relayData = */
          _this.open_music = relayData.settings.open_music
          let list = JSON.parse(JSON.stringify(relayData.settings.part_images))
          for(let i = 0;i<list.length;i++){
            let demo = list[i]
            demo.image = checkImage(1,demo.image)
          }
          let listPoster = JSON.parse(JSON.stringify(relayData.settings.poster_diagram))
          for(let i = 0;i<listPoster.length;i++){
            let demo = listPoster[i]
            demo.image = checkImage(1,demo.image)
          }
          _this.$set(relayData.settings,"part_images", list)
          _this.$set(relayData.settings,"poster_diagram", listPoster)
          _this.relayData = relayData
          uni.setStorageSync('platform',JSON.stringify(relayData.settings.platform))
          if(relayData.popup){
            switch(relayData.popup){
              case 'home':
                _this.$store.state.step = 'index'
                break;
              case 'content':
                _this.$store.state.step = 'view'
                break;
              case 'adver':
                _this.$store.state.step = 'sign'
                break;
              case 'poster':
                _this.$store.state.step = 'poster'
                break;
            }
          }else{
            _this.$store.state.step = 'index'
          }

        }

      })
    },
    async getInitData() {
      let res = await ActivityInfo({actid: this.actid})
      this.$setTitle(res.data.title)
      uni.setStorageSync('title',res.data.title)
      let relayData = res.data
      this.relayData = relayData
      if(0 == relayData.status){
        watermarkOption.set('活动未发布')
      }
	  
	  let _this = this
	  this.$nextTick(()=>{
		  let token = localStorage.getItem(_this.actid+'bearerToken') || null
		  if(token!=null && _this.$store.state.long==0 && _this.$store.state.lat==0  &&_this.relayData.area && _this.relayData.area.type==3){
		  			_this.$store.dispatch('getPos')
		  			window.getLocationCallBackFun=()=>{}
		  }
	  })
		  
	  
      this.showData = true
      this.open_music = res.data.settings.open_music
      uni.setStorageSync('platform',JSON.stringify(relayData.settings.platform))
      // this.$store.commit('SET_MAIN_COLOR', res.data.settings.bgcolor)
      setTimeout(() => {
        _this.$store.commit('SET_MAIN_COLOR', res.data.settings.bgcolor)
      }, 300);
      this.$nextTick(() => {
        let data =  res.data
		proxyShareFun(data.settings.title||data.title,data.settings.image,data.settings.desc,window.location.href)
        this.$refs['audioCon'].addEvent()
      })
    },
    setPosterShare(data,needed){
      shareFun(data.settings.title||data.title,data.settings.image,data.settings.desc,window.location.href,null,()=>{this.$refs['audioCon'].addEvent()})
    }
  },
  props:{

  },
  created(op) {

  },
  mounted() {
    this.actid = uni.getStorageSync('actid')
    this.$store.commit('SETACTIVITYNAME', 'poster')
    let web = uni.getStorageSync('WEB') || null
    if (web) {
      /*兼容后台模拟数据*/
      this.isframe = true
    }
    this.getInit()

  },

}
</script>

<style scoped lang="scss">

.v-enter-active,
.v-leave-active {
  transition: opacity 0.15s ease;
}

.v-enter-from,
.v-leave-to {
  opacity: 0.5;
}

@import '../../../components/relay/scss/relay.scss';
body {
  /*让 body 初始 width 和 height 就 等于 页面可视区域的 宽高*/
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}

.relayArea {
  width: 100%;
  min-height: 100vh;
  position: relative;
  /*background: url("../../../static/relay/relay_bac.png");
  background-size: cover;
  background-repeat: no-repeat;
  overflow: hidden;*/
}

</style>
